package startup;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.swtdesigner.SWTResourceManager;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.MouseMoveListener;

public class CMP {

	protected Shell shell;

	private final static int NUM=10;
	private Composite[] cmps = new Composite[NUM];
	private ScrolledComposite scrolledComposite;
	private int i=0;
	private Composite current=null;
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			CMP window = new CMP();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.addMouseMoveListener(new MouseMoveListener() {
			public void mouseMove(MouseEvent e) {
				//System.out.println("mouse move vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
			}
		});
		shell.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				System.out.println("lsot shell focus");
			}
		});
		shell.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseExit(MouseEvent e) {
				System.out.println("Leave shell");
				if(current!=null){
					Control[] ctr = current.getChildren();
					for (int i = 0; i < ctr.length; i++) {
						if (ctr[i] instanceof Button) {
							// System.out.println("Button   exit");
							ctr[i].setVisible(false);
							//ctr[i].setEnabled(false);
							// ctr[i].pack();
						}
					}
					current = null;
				}
			}
			@Override
			public void mouseEnter(MouseEvent e) {
				System.out.println("enter shell");
				if(current!=null){
					Control[] ctr = current.getChildren();
					for (int i = 0; i < ctr.length; i++) {
						if (ctr[i] instanceof Button) {
							// System.out.println("Button   exit");
							ctr[i].setVisible(false);
							//ctr[i].setEnabled(false);
							// ctr[i].pack();
						}
					}
					current = null;
				}
			}
			@Override
			public void mouseHover(MouseEvent e) {
			}
		});
		shell.setSize(419, 470);
		shell.setText("SWT Application");
		
		scrolledComposite = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseExit(MouseEvent e) {
				System.out.println("Leave scroll");
			}
			@Override
			public void mouseEnter(MouseEvent e) {
				System.out.println("Enter scroll");
				if(current!=null){
					Control[] ctr = current.getChildren();
					for (int i = 0; i < ctr.length; i++) {
						if (ctr[i] instanceof Button) {
							// System.out.println("Button   exit");
							ctr[i].setVisible(false);
							//ctr[i].setEnabled(false);
							// ctr[i].pack();
						}
					}
					current = null;
				}
			}
		});
		scrolledComposite.setMinHeight(100);
		scrolledComposite.setBounds(10, 5, 391, 411);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		Composite composite = new Composite(scrolledComposite, SWT.NONE);
		composite.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseExit(MouseEvent e) {
				System.out.println("Leave composite   XXXXXXXXXXXXXXXXXXXXXX");
			}
		});
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		//composite.setSize(401,1500);
		composite.setBounds(80,0,401,1500);
		
		
		
		FillLayout fl =new FillLayout(SWT.VERTICAL);
		fl.marginHeight = 0;
		fl.marginWidth = 0;
		fl.spacing = 0;
		composite.setLayout(fl);
//		Button b = new Button(composite,SWT.NONE);
//		b.setText("button1");
//		//b.pack();
//		Button b2 = new Button(composite,SWT.NONE);
//		b2.setText("button2");
//		//b2.pack();
//		Button b3 = new Button(composite,SWT.NONE);
//		b3.setText("button3");
//		b3.pack();
//		Button b4 = new Button(composite,SWT.NONE);
//		b4.setText("button4");
//		//b4.pack();
		
		
		
		for(int i=0;i<NUM;i++){
			cmps[i] = new Composite(composite,SWT.NONE);
			System.out.println(cmps[i].getSize());
			//cmps[i].setSize(400,150);
			cmps[i].setBounds(100, 0, 400, 100);
			cmps[i].setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_CYAN));
			//cmps[i].addMouseTrackListener(new MouseTrackListener());
			System.out.println(cmps[i].getSize());
			//cmps[i].pack();
			//cmps[i].computeSize(300, 100);
			new Text(cmps[i],SWT.None).setBounds(0,0,0,100);
			
			final Button b1=new Button(cmps[i],SWT.NONE);
			final Button b2=new Button(cmps[i],SWT.NONE);
			final Button b3=new Button(cmps[i],SWT.NONE);
			//b1 = new Button(cmps[i],SWT.NONE);
			b1.setBounds(300, 10, 30, 20);
			b1.setText("button1");
			b1.setVisible(false);
			//b1.pack();
			//b2 = new Button(cmps[i],SWT.NONE);
			b2.setBounds(300, 40, 30, 20);
			b2.setText("button2");
			b2.setVisible(false);
			//b2.pack();
			//b3 = new Button(cmps[i],SWT.NONE);
			b3.setBounds(300, 70, 30, 20);
			b3.setText("button3");
			b3.setVisible(false);
			cmps[i].addMouseTrackListener(new MouseTrackAdapter() {
				public void mouseEnter(MouseEvent e) {
				Composite c = (Composite)e.widget;
				if(current == null){
					current = c;
				}else{
						if (current != c) {
							Control[] ctr = current.getChildren();
							for (int i = 0; i < ctr.length; i++) {
								if (ctr[i] instanceof Button) {
									// System.out.println("Button   exit");
									ctr[i].setVisible(false);
									//ctr[i].setEnabled(false);
									// ctr[i].pack();
								}
							}
							current = c;
						}
				}
				if(cmps[1] == c){
					//System.out.println("same");
				}
				//System.out.println("Button   enter " +e);
				//System.out.println(c);
				//System.out.println("Button enter");
				Control[] ctr = c.getChildren();
				for(int i=0;i<ctr.length;i++){
					if(ctr[i] instanceof Button){
						
						ctr[i].setVisible(true);
					}
				}
				
				return;
			}
			@Override
			public void mouseExit(MouseEvent e) {
				Composite c = (Composite)e.widget;
				System.out.println(c+"  "+e);
//				if(current == null){
//					return;
//				}else{
//						if (current != c) {
//							Control[] ctr = current.getChildren();
//							for (int i = 0; i < ctr.length; i++) {
//								if (ctr[i] instanceof Button) {
//									// System.out.println("Button   exit");
//									ctr[i].setVisible(false);
//									//ctr[i].setEnabled(false);
//									// ctr[i].pack();
//								}
//							}
//							current = c;
//						}
//				}
//				//cmps[i].setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
//				Composite c = (Composite)e.widget;
//				if(cmps[1] == c){
//					System.out.println("exit   same ");
//				}
//				System.out.println("Button exit " +e);
//				Control[] ctr = c.getChildren();
//				for(int i=0;i<ctr.length;i++){
//					if(ctr[i] instanceof Button){
//						//System.out.println("Button   exit");
//						//ctr[i].setVisible(false);
//						ctr[i].setEnabled(false);
//						//ctr[i].pack();
//					}
//				}
			}
			@Override
			public void mouseHover(MouseEvent e) {
				
				
			}
		});
			
//			cmps[i].addMouseTrackListener(new MouseTrackAdapter() {
//				public void mouseEnter(MouseEvent e) {
//					Composite c = (Composite)e.widget;
//					//System.out.println(c);
//					//System.out.println("Button enter");
//					Control[] ctr = c.getChildren();
//					for(int i=0;i<ctr.length;i++){
//						if(ctr[i] instanceof Button){
//							System.out.println("Button   enter");
//							ctr[i].setVisible(true);
//						}
//					}
//					//composite.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_RED));
//					//cmps[i].setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_CYAN));
//					b1.setVisible(true);
//					//b2.setVisible(true);
//					//b3.setVisible(true);
//					b1.pack();
//				}
//				@Override
//				public void mouseExit(MouseEvent e) {
//					//cmps[i].setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
//					System.out.println("Button exit");
//					b1.setVisible(false);
//					b2.setVisible(false);
//					b3.setVisible(false);
//					b1.pack();
//				}
//				@Override
//				public void mouseHover(MouseEvent e) {
//					
//					
//				}
//			});
			
			//cmps[i].setMinSize(cmps[i].computeSize(300, 100););
			//System.out.println(cmps[i].getSize());
			cmps[i].pack();
			//cmps[i].layout();
		}
//		Composite c2 = new Composite(cmps[1],SWT.None);
//		c2.setBounds(345, 0, 30, 100);
//		final Button b1=new Button(c2,SWT.NONE);
//		final Button b2=new Button(c2,SWT.NONE);
//		final Button b3=new Button(c2,SWT.NONE);
//		Composite c1 = new Composite(cmps[1],SWT.None);
//		c1.setBounds(0, 0, 335, 100);
		
/*		
		
		final Button b1=new Button(cmps[1],SWT.NONE);
		final Button b2=new Button(cmps[1],SWT.NONE);
		final Button b3=new Button(cmps[1],SWT.NONE);
		//Composite c1 = new Composite(cmps[1],SWT.None);
		
		//b1 = new Button(cmps[i],SWT.NONE);
		b1.setBounds(300, 10, 30, 20);
		b1.setText("button1");
		b1.setVisible(false);
		b1.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(MouseEvent e) {
				System.out.println(e.widget);
			 
		}
		@Override
		public void mouseExit(MouseEvent e) {
			
		}
		@Override
		public void mouseHover(MouseEvent e) {
			
			
		}
	});
		//b1.pack();
		//b2 = new Button(cmps[i],SWT.NONE);
		b2.setBounds(300, 40, 30, 20);
		b2.setText("button2");
		b2.setVisible(false);
		//b2.pack();
		//b3 = new Button(cmps[i],SWT.NONE);
		b3.setBounds(300, 70, 30, 20);
		b3.setText("button3");
		b3.setVisible(false);
		cmps[1].addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(MouseEvent e) {
			Composite c = (Composite)e.widget;
			if(current == null){
				current = c;
			}else{
				if(current !=c){
				Control[] ctr = current.getChildren();
				for(int i=0;i<ctr.length;i++){
					if(ctr[i] instanceof Button){
						//System.out.println("Button   exit");
						//ctr[i].setVisible(false);
						ctr[i].setEnabled(false);
						//ctr[i].pack();
					}
				}
				}
			}
			if(cmps[1] == c){
				//System.out.println("same");
			}
			System.out.println("Button   enter " +e);
			//System.out.println(c);
			//System.out.println("Button enter");
			Control[] ctr = c.getChildren();
			for(int i=0;i<ctr.length;i++){
				if(ctr[i] instanceof Button){
					
					ctr[i].setVisible(true);
				}
			}
			
			return;
		}
		@Override
		public void mouseExit(MouseEvent e) {
//			//cmps[i].setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
//			Composite c = (Composite)e.widget;
//			if(cmps[1] == c){
//				System.out.println("exit   same ");
//			}
//			System.out.println("Button exit " +e);
//			Control[] ctr = c.getChildren();
//			for(int i=0;i<ctr.length;i++){
//				if(ctr[i] instanceof Button){
//					//System.out.println("Button   exit");
//					//ctr[i].setVisible(false);
//					ctr[i].setEnabled(false);
//					//ctr[i].pack();
//				}
//			}
		}
		@Override
		public void mouseHover(MouseEvent e) {
			
			
		}
	});
*/
		//composite.pack();
		//scrolledComposite.setContent(composite);
		scrolledComposite.setContent(composite);
		scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		
		
	}
	
	private class MouseTrackListener extends MouseTrackAdapter {
		Button b1=null;
		Button b2=null;
		Button b3=null;
		public void mouseEnter(MouseEvent e) {
			Composite c = (Composite)e.widget;
			System.out.println(c);
			Control[] ctr = c.getChildren();
			for(Control o:ctr){
				if(o instanceof Button){
					System.out.println("Button   enter");
					o.setVisible(true);
				}
				//System.out.println(o.getClass());
				//System.out.println(o);
			}
//			b1 = new Button(c,SWT.NONE);
//			b1.setBounds(300, 10, 30, 20);
//			b1.setText("button1");
//			//b1.pack();
//			b2 = new Button(c,SWT.NONE);
//			b2.setBounds(300, 40, 30, 20);
//			b2.setText("button2");
//			//b2.pack();
//			b3 = new Button(c,SWT.NONE);
//			b3.setBounds(300, 70, 30, 20);
//			b3.setText("button3");
			//b3.pack();
			//composite.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_RED));
			//cmp.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_CYAN));
			//b1.setVisible(true);
			//b2.setVisible(true);
			//b3.setVisible(true);
			//scrolledComposite.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
			//c.pack();
		}
		@Override
		public void mouseExit(MouseEvent e) {
			//cmp.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
			//b1.setVisible(false);
			//b2.setVisible(false);
			//b3.setVisible(false);
//			Composite c = (Composite)e.widget;
//			
//			Control[] ctr = c.getChildren();
//			for(Control o:ctr){
//				if(o instanceof Button){
//					System.out.println("Button exit");
//					o.setVisible(false);
//				}
//			}
		}
		@Override
		public void mouseHover(MouseEvent e) {
			
			
		}
	}
}
